package com.example.datahub;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class PackageNameExtractor {
    public static void main(String[] args) throws Exception {
        List<String> packageNames = getAllPackageNames();

        for (String packageName : packageNames) {
            System.out.println(packageName);
        }
       /* new Thread(()->{
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();*/
    }

    private static List<String> getAllPackageNames9() throws ClassNotFoundException, URISyntaxException, IOException {
        List<String> packageNames = new ArrayList<>();
        Package[] packages = Package.getPackages();
        for (Package aPackage : packages) {
            packageNames.add(aPackage.getName());
        }
        return packageNames;
    }
    private static List<String> getAllPackageNames() throws ClassNotFoundException, URISyntaxException, IOException {
        Set<String> packageNames = new HashSet<>();
        String classPath = System.getProperty("java.class.path");
        System.out.println(classPath);
        for (String url : classPath.split(";")) {
            File file = new File(url);

            if (!file.exists()) {
                continue;
            }

            String path = file.getAbsolutePath();

            // 如果是目录则递归查找子目录
            if (path.endsWith(".jar")) {
                processJarFile(new JarFile(file), packageNames);
            } else {
                processDirectory(file, packageNames);
            }
        }
        return Arrays.asList(packageNames.toArray(new String[packageNames.size()]));
    }

    private static void processJarFile(JarFile jarFile, Set<String> packageNames) throws IOException {
        Enumeration<JarEntry> entries = jarFile.entries();

        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();

            if (entry.getName().endsWith(".class") &&  !entry.isDirectory()) {
                String className = entry.getName().replace('/', '.').substring(0, entry.getName().length() - ".class".length());

                int lastDotIndex = className.lastIndexOf('.');

                if (lastDotIndex != -1) {
                    String packageName = className.substring(0, lastDotIndex);

                    if (!packageNames.contains(packageName)) {
                        packageNames.add(packageName);
                    }
                }
            }
        }
    }

    private static void processDirectory(File directory, Set<String> packageNames) {
        File[] files = directory.listFiles((dir, name) -> name.endsWith(".class"));

        if (files != null) {
            for (File file : files) {
                String fileName = file.getName();

                if (fileName.endsWith(".class")) {
                    String className = fileName.substring(0, fileName.length() - ".class".length()).replace('\\', '.');

                    int lastDotIndex = className.lastIndexOf('.');

                    if (lastDotIndex != -1) {
                        String packageName = className.substring(0, lastDotIndex);

                        if (!packageNames.contains(packageName)) {
                            packageNames.add(packageName);
                        }
                    }
                }
            }
        }
    }
}